// top level await is available in ES modules loaded from script tags

chrome.runtime.onMessage.addListener((message, sender) => {
  // The callback for runtime.onMessage must return falsy if we're not sending a response
  (async () => {
    console.log(message, "--message-----");
    if (message.text) {
      document.querySelector("#SelectionContent").value = message.text;
      let frame = window.frames[0]
      frame.postMessage(
        {
          info: message.text,
        },
        '*'
      )
    }
    if (message.url) {
      document.querySelector("#urlContent").value = message.url;
    }
  })();
});
document.querySelector("#send").addEventListener("mouseup", function (event) {
  console.log("send");
  let val = document.querySelector("#inputContent").value;
  // chrome.runtime.sendMessage();
  sendMessage({ pop: val });
});

function sendMessage(info) {
  // popup ---> content
  chrome.tabs.query(
    {
      active: true,
      currentWindow: true,
    },
    (tabs) => {
      chrome.tabs.sendMessage(tabs[0].id, info, (res) => {
        console.log("popup=>content");
        console.log(res);
      });
    }
  );
}
